<template>
  <ContentWrap>
    <!-- 搜索工作栏 -->
    <el-form
      class="-mb-15px"
      :model="queryParams"
      ref="queryFormRef"
      :inline="true"
      label-width="68px"
    >
      <el-form-item label="采购日期" prop="createTime">
        <el-date-picker
          v-model="queryParams.createTime"
          value-format="YYYY-MM-DD HH:mm:ss"
          type="daterange"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          :default-time="[new Date('1 00:00:00'), new Date('1 23:59:59')]"
          class="!w-220px"
        />
      </el-form-item>
      <el-form-item label="单号" prop="supplierName">
        <el-input
          v-model="queryParams.supplierName"
          placeholder="请输入单号"
          clearable
          @keyup.enter="handleQuery"
          class="!w-180px"
        />
      </el-form-item>
      <el-form-item label="供应商编号" label-width="100" prop="supplierName">
        <el-input
          v-model="queryParams.supplierName"
          placeholder="请输入供应商编号"
          clearable
          @keyup.enter="handleQuery"
          class="!w-180px"
        />
      </el-form-item>
      <el-form-item label="供应商名称" label-width="100" prop="supplierName">
        <el-input
          v-model="queryParams.supplierName"
          placeholder="请输入供应商名称"
          clearable
          @keyup.enter="handleQuery"
          class="!w-180px"
        />
      </el-form-item>
      <el-form-item>
        <el-button @click="handleQuery"><Icon icon="ep:search" class="mr-5px" /> 搜索</el-button>
        <el-button @click="resetQuery"><Icon icon="ep:refresh" class="mr-5px" /> 重置</el-button>
      </el-form-item>
      <div style="display: flex; justify-content: space-between;">
        <el-form-item>
          <el-button
            type="primary"
            plain
            @click="getList"
          >
            <Icon icon="ep:refresh" class="mr-5px" /> 刷新
          </el-button>
        </el-form-item>
        <el-form-item>
          <el-button
            type="primary"
            plain
            @click="deleteItems"
          >
            <Icon icon="ep:minus" class="mr-5px" /> 删除
          </el-button>
          <el-button
            type="primary"
            plain
            @click="openForm('check')"
            v-hasPermi="['erp:pur-raw-material-purchase-order:create']"
          >
            <Icon icon="ep:check" class="mr-5px" /> 审核
          </el-button>
        </el-form-item>
      </div>
    </el-form>
  </ContentWrap>

  <!-- 列表 -->
  <ContentWrap>
    <el-tabs
      v-model="tabSelected"
    >
      <el-tab-pane label="明细" name="detail">
        <ReusableTable
          :tableData="list"
          :columns="columnsdetail"
          :loading="loading"
          @button-click="handleTableButton"
          @selection-change="handleSelectionChange"
          @filter-change="handleFilterChange"
        />
        <!-- 分页 -->
        <Pagination
          :total="total"
          v-model:page="queryParams.pageNo"
          v-model:limit="queryParams.pageSize"
          @pagination="getList"
        />
      </el-tab-pane>
      <el-tab-pane label="视图" name="overall">
        <ReusableTable
          :tableData="list"
          :columns="columnsoverall"
          :loading="loading"
          @button-click="handleTableButton"
          @selection-change="handleSelectionChange"
        />
        <!-- 分页 -->
        <Pagination
          :total="total"
          v-model:page="queryParams.pageNo"
          v-model:limit="queryParams.pageSize"
          @pagination="getList"
        />
      </el-tab-pane>
    </el-tabs>
  </ContentWrap>

  <!-- 表单弹窗：添加/修改 -->
  <PurRawMaterialPurchaseOrderForm ref="formRef" @success="getList" />
</template>

<script setup lang="ts">
import { dateFormatter } from '@/utils/formatTime'
import download from '@/utils/download'
import { PurRawMaterialPurchaseOrderApi, PurRawMaterialPurchaseOrderVO } from '@/api/erp/purrawmaterialpurchaseorder'
import PurRawMaterialPurchaseOrderForm from './PurRawMaterialPurchaseOrderForm.vue'

/** ERP采购管理-原料采购单主 列表 */
defineOptions({ name: 'PurRawMaterialPurchaseOrder' })

const message = useMessage() // 消息弹窗
const { push } = useRouter() // 路由对象
const { t } = useI18n() // 国际化

const loading = ref(true) // 列表的加载中
const list = ref<PurRawMaterialPurchaseOrderVO[]>([]) // 列表的数据
const total = ref(0) // 列表的总页数
const multipleSelection = ref([]) // 选中行
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  purchaseOrderNo: undefined,
  supplierName: undefined,
  purchaseType: undefined,
  isApproved: undefined,
  createTime: []
})
const queryFormRef = ref() // 搜索的表单
const exportLoading = ref(false) // 导出的加载中

const tabSelected = ref('detail')

const filters1 = ref([
        { text: '1', value: 1 },
        { text: '2', value: 2 },
        { text: '3', value: 3 },
        { text: '4', value: 4 },
      ])

const columnsdetail = reactive([
  { label: '', prop: '', fixed: true, type: 'selection' },
  // { label: 'ID', prop: 'id', fixed: true, width: 160, buttons: [
  //   { action: 'edit', type: 'primary', link: true }
  // ] },
  { label: '采购单号', prop: 'purchaseOrderNo', fixed: true, width: 120, buttons: [
    { action: 'edit', type: 'primary', link: true }
  ] },
  { label: '供应商编号', prop: 'supplierId', width: '120px' },
  { label: '供应商名称', prop: 'supplierName', width: '120px' },
  { label: '采购日期', prop: 'approveTime', type: 'date', width: '120' },
  { label: '交货日期', prop: 'approveTime', type: 'date', width: '120' },
  { label: '制单人', prop: 'approveTime', width: '120', filters: filters1 },
  { label: '备注', prop: 'purchaseType', align: 'center' },
  { label: '采购总金额', prop: 'purchaseType', width: 140 },
  { label: '报价单号', prop: 'purchaseType', align: 'center' },
  { label: '总重量', prop: 'totalWeight' },
  { label: '总面积', prop: 'totalArea' },
  { label: '总体积', prop: 'totalVolume' },
  { label: '打印', prop: 'purchaseType', align: 'center' },
  { label: '审核', prop: 'isApproved', align: 'center' },
  { label: '审核人', prop: 'approver', align: 'center' },
  { label: '审核时间', prop: 'approveTime', type: 'date', width: '120' },
  { label: '采购类型', prop: 'purchaseType', align: 'center' },
  { label: '创建时间', prop: 'createTime', type: 'date', width: '120' },
  { label: '超时', prop: 'discount' },
  { label: '基础金额', prop: 'basicAmount' },
  { label: '折扣', prop: 'discount' },
  { label: '折后金额', prop: 'discountedAmount' },
  { label: '折扣金额', prop: 'discountAmount' },
  { label: '税率', prop: 'taxRate' },
  { label: '税额', prop: 'taxAmount' },
  { label: '税别', prop: 'taxType' },
  {
    label: '操作',
    prop: 'action',
   
    slot: 'action',
    fixed: 'right',
    width: 200,
    buttons: [
      { text: '编辑', action: 'edit', type: 'primary', link: true },
      { text: '删除', action: 'delete', type: 'danger', link: true }
    ]
  }
])
const columnsoverall = reactive([
  { label: '', prop: '', fixed: true, type: 'selection' },
  { label: '采购单号', prop: 'purchaseOrderNo', width: '120px' },
  { label: '供应商编号', prop: 'supplierId', width: '120px' },
  { label: '供应商名称', prop: 'supplierName', width: '120px' },
  { label: '月结', prop: 'supplierName', align: 'center' },
  { label: '采购日期', prop: 'approveTime', type: 'date', width: '120' },
  { label: '交货日期', prop: 'approveTime', type: 'date', width: '120' },
  { label: '生产单号', prop: 'purchaseOrderNo', width: '120px' },
  { label: '客户名称', prop: 'purchaseOrderNo', width: '120px' },
  { label: '采购材质', prop: 'purchaseOrderNo', align: 'center' },
  { label: '采购宽', prop: 'purchaseOrderNo', align: 'center' },
  { label: '采购长', prop: 'purchaseOrderNo', align: 'center' },
  { label: '采购单价', prop: 'purchaseOrderNo', align: 'center' },
  { label: '面积单价', prop: 'purchaseOrderNo', align: 'center' },
  { label: '采购数', prop: 'purchaseOrderNo', align: 'center' },
  { label: '采购金额', prop: 'purchaseOrderNo', align: 'center' },
  { label: '采购员', prop: 'purchaseOrderNo', align: 'center' },
  { label: '采购规格', prop: 'purchaseOrderNo', align: 'center' },
  { label: '材质类型', prop: 'purchaseOrderNo', align: 'center' },
  { label: '单个面积', prop: 'purchaseOrderNo', align: 'center' },
  { label: '已到数', prop: 'purchaseOrderNo', align: 'center' },
  { label: '压线', prop: 'purchaseOrderNo', align: 'center' },
  { label: '压线类型', prop: 'purchaseOrderNo', align: 'center' },
  { label: '客户PO号', prop: 'purchaseOrderNo', width: 120 },
  { label: '产品货号', prop: 'purchaseOrderNo', align: 'center' },
  { label: '产品品名', prop: 'purchaseOrderNo', align: 'center' },
  { label: '产品品名', prop: 'purchaseOrderNo', align: 'center' },
  { label: '产品规格', prop: 'purchaseOrderNo', align: 'center' },
  { label: '产品款式', prop: 'purchaseOrderNo', align: 'center' },
  { label: '送货类型', prop: 'purchaseOrderNo', align: 'center' },
  { label: '采购类型', prop: 'purchaseOrderNo', align: 'center' },
  { label: '供应商材质名称', prop: 'purchaseOrderNo', width: '140' },
  { label: '厚度', prop: 'purchaseOrderNo', align: 'center' },
  { label: '克重', prop: 'purchaseOrderNo', align: 'center' },
  { label: '主交货日期', prop: 'purchaseOrderNo', width: 120, type: 'date' },
  { label: '优惠', prop: 'purchaseOrderNo', align: 'center' },
  { label: '附加费', prop: 'purchaseOrderNo', align: 'center' },
  { label: '对照名称', prop: 'purchaseOrderNo', align: 'center' },
  { label: '客户手工单号', prop: 'purchaseOrderNo', width: 140 },
  { label: '备注1', prop: 'purchaseType', align: 'center' },
  { label: '备注2', prop: 'purchaseType', align: 'center' },
  { label: '备注3', prop: 'purchaseType', align: 'center' },
  { label: '备注4', prop: 'purchaseType', align: 'center' },
  { label: '备注5', prop: 'purchaseType', align: 'center' },
  { label: '备注6', prop: 'purchaseType', align: 'center' },
  {
    label: '操作',
    prop: 'action',
   
    slot: 'action',
    fixed: 'right',
    width: 200,
    buttons: [
      { text: '编辑', action: 'edit', type: 'primary', link: true },
      { text: '删除', action: 'delete', type: 'danger', link: true }
    ]
  }
])

/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    const data = await PurRawMaterialPurchaseOrderApi.getPurRawMaterialPurchaseOrderPage(queryParams)
    list.value = [...data.list, {approveTime: 1}, {approveTime: 2}, {approveTime: 3}, {approveTime: 1}, {approveTime: 1}]
    total.value = data.total
  } finally {
    loading.value = false
  }
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value.resetFields()
  handleQuery()
}

/** 添加/修改操作 */
const formRef = ref()
const openForm = (type: string, id?: number) => {
  push({ path: 'purrawmaterialpurchaseorderdetail', state: { type: type, id: id } })
}

/** 删除按钮操作 */
const handleDelete = async (id: number) => {
  try {
    // 删除的二次确认
    await message.delConfirm()
    // 发起删除
    await PurRawMaterialPurchaseOrderApi.deletePurRawMaterialPurchaseOrder(id)
    message.success(t('common.delSuccess'))
    // 刷新列表
    await getList()
  } catch {}
}

const deleteItems = async () => {
  // console.log(multipleSelection.value)
  try {
    // 删除的二次确认
    await message.delConfirm()
    // 发起删除
    // await PurRawMaterialPurchaseOrderApi.deletePurRawMaterialPurchaseOrder()
    message.success(t('common.delSuccess'))
    // 刷新列表
    await getList()
  } catch {}
}

/** 导出按钮操作 */
const handleExport = async () => {
  try {
    // 导出的二次确认
    await message.exportConfirm()
    // 发起导出
    exportLoading.value = true
    const data = await PurRawMaterialPurchaseOrderApi.exportPurRawMaterialPurchaseOrder(queryParams)
    download.excel(data, 'ERP采购管理-原料采购单主.xls')
  } catch {
  } finally {
    exportLoading.value = false
  }
}

/** 审核 */
const handleCheck = (id: number) => {
  console.log(id)
}

/** 表头过滤 */
const handleFilterChange = () => {}

/** 选中行操作 */
const handleSelectionChange = (val) => {
  multipleSelection.value = val
}

const handleTableButton = ({ action, row }) => {
  switch (action) {
    case 'edit':
      openForm('edit', row.id)
      break
    case 'delete':
      handleDelete(row.id)
      break
    case 'refresh':
      getList()
      break
    case 'check':
      handleCheck(row.id)
      break
    default:
      console.warn(`未处理的操作: ${action}`)
  }
}

/** 初始化 **/
onMounted(() => {
  getList()
})
</script>